package gamehistory.player;

import gamehistory.HistoryAssimilation;

import java.util.Collection;
import java.util.HashMap;

import scg.gen.AccountAcceptTrans;
import scg.gen.AccountRefundTrans;
import scg.gen.AccountSolveTrans;
import scg.gen.History;
import scg.gen.PlayerID;
import scg.gen.PlayerKickedEvent;
import scg.gen.PlayerSpec;
import scg.gen.PlayerTrans;
import edu.neu.ccs.demeterf.lib.Entry;

/**
 * 
 * @author Brent Kersanske
 *
 */
public class PlayerModelAssimilation extends HistoryAssimilation {
	
	private static final String PLAYERS_FILE_END = "_Players";
	
	private static final String PLAYERS = "players";
	private HashMap<Integer, PlayerModel> players;

	/**
	 * Constructor.
	 * @param gameHistory
	 */
	public PlayerModelAssimilation(History gameHistory) {
		super(PLAYERS);
		assimilate(gameHistory);
	}
	
	/**
	 * Gets the number of players incorporated into this assimilation.
	 * @return
	 */
	public int getNumberOfPlayers() {
		return this.players.values().size();
	}
		
	/**
	 * 
	 * @return
	 */
	public Collection<PlayerModel> getPlayerModels() {
		return players.values();
	}
	
	/**
	 * 
	 * @param playerID
	 * @return
	 */
	public PlayerModel getPlayerModel(Integer playerID) {
		return this.players.get(playerID);
	}
	
	/**
	 * {@inheritDoc}
	 */
	protected void assimilate(History gameHistory) {
		this.players = new HashMap<Integer, PlayerModel> ();
		for(Entry<PlayerID,PlayerSpec> playerEntry : gameHistory.getHeader().getPlayers()) {
			PlayerModel PlayerModel = new PlayerModel(playerEntry.getKey().getId(), playerEntry.getVal().getName());
			this.players.put(playerEntry.getKey().getId(), PlayerModel);			
		}
		super.assimilate(gameHistory);
	}	

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void handlePlayerTransactions(int roundNumber, PlayerTrans playerTransactions) {
		PlayerModel PlayerModel = players.get(playerTransactions.getId().getId());
		PlayerModel.assimilatePlayerTransactions(roundNumber, playerTransactions);
	}	

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void handleAccountAcceptTransaction(int roundNumber, AccountAcceptTrans accountAcceptTrans) {
		PlayerModel acceptorProfile = this.players.get(accountAcceptTrans.getAcceptor());
		acceptorProfile.assimilateAccountAcceptTransactionForAcceptor(roundNumber, accountAcceptTrans);		
		PlayerModel offererProfile = this.players.get(accountAcceptTrans.getOfferer());
		offererProfile.assimilateAccountAcceptTransactionForOfferer(roundNumber, accountAcceptTrans);
	}	

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void handleAccountSolveTransaction(int roundNumber, AccountSolveTrans accountSolveTrans) {
		PlayerModel solverProfile = this.players.get(accountSolveTrans.getAcceptor());
		solverProfile.assimilateAccountSolveTransactionForSolver(roundNumber, accountSolveTrans);
		PlayerModel offerProfile = this.players.get(accountSolveTrans.getOfferer());
		offerProfile.assimilateAccountSolveTransactionForOfferer(roundNumber, accountSolveTrans);			
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void handleAccountRefundTransaction(int roundNumber, AccountRefundTrans accountRefundTrans) {
		// TODO Auto-generated method stub		
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void handlePlayerKicked(PlayerKickedEvent playerKickedEvent) {
		// TODO Auto-generated method stub		
	}
	
	@Override
	protected void buildXMLModel() {
		for(PlayerModel player : this.players.values()) {
			player.buildModel();
			this.assimilatedModel.addChildModel(player);
		}
	}

	@Override
	protected String getAssimilationType() {
		return PLAYERS;
	}

	@Override
	public String retrieveFileOutName() {
		return PLAYERS_FILE_END;
	}
}
